package com.beordie.controller;

import com.github.pagehelper.PageInfo;
import com.beordie.model.Team;
import com.beordie.service.TeamService;
import com.beordie.bean.TeamBean;
import com.beordie.bean.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

/**
 * 球队控制器层
 */

@Controller
@RequestMapping("team")
@ResponseBody
public class TeamController {
    @Resource
    private TeamService teamService;

    /**
     * @description 分页查询所有球队信息
     * @author 30500
     * @date 2021/7/24 8:19
     * @type [java.lang.Integer, java.lang.Integer, com.beordie.bean.TeamBean]
     * @return com.beordie.bean.Message<com.beordie.model.Team>
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Message<Team> queryByPage(Integer pageNum, Integer pageSize, TeamBean vo) {
        System.out.println(vo);
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Team> teamPageInfo = teamService.queryByPage(pageNum, pageSize, vo);
        Message message = new Message();
        if (teamPageInfo != null) {
            message.setMsg("删除成功");
            message.setPageInfo(teamPageInfo);
        } else {
            message.setMsg("服务器异常，请稍后再试");
            message.setCode(500);
        }
        return message;
    }

    /**
     * @description 根据主键删除数据（逻辑删除）
     * @author 30500
     * @date 2021/7/24 8:19
     * @type [int]
     * @return com.beordie.bean.Message
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Message deleteById(@PathVariable("id") int teamId) {
        int delete = teamService.deleteById(teamId);
        Message message = new Message();
        if (delete > 0)
            message.setMsg("删除成功");
        else {
            message.setMsg("服务器异常，请稍后再试");
            message.setCode(500);
        }
        return message;
    }

    /**
     * @description 添加一条球队记录
     * @author 30500
     * @date 2021/7/24 8:20
     * @type [com.beordie.model.Team]
     * @return com.beordie.bean.Message
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public Message insertTeam(Team team) {
        int insert = teamService.insertTeam(team);
        Message message = new Message();
        if (insert > 0)
            message.setMsg("插入成功");
        else {
            message.setMsg("服务器异常，请稍后再试");
            message.setCode(500);
        }
        return message;
    }

    /**
     * @description 根据主键ID更新球队信息
     * @author 30500
     * @date 2021/7/24 8:20
     * @type [int, com.beordie.model.Team]
     * @return com.beordie.bean.Message
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Message updateTeam(@PathVariable("id") int teamId, Team team) {
        team.setTeamId(teamId);
        int update = teamService.updateById(team);
        Message message = new Message();
        if (update > 0)
            message.setMsg("插入成功");
        else {
            message.setMsg("服务器异常，请稍后再试");
            message.setCode(500);
        }
        return message;
    }

    /**
     * @description 根据主键查询球队信息
     * @author 30500
     * @date 2021/7/24 8:21
     * @type [int]
     * @return com.beordie.bean.Message<com.beordie.model.Team>
     */
    @RequestMapping("/{id}")
    public Message<Team> queryById(@PathVariable("id") int teamId) {
        Team team = teamService.queryById(teamId);
        Message message = new Message();
        if (team != null) {
            message.setMsg("查询成功");
            message.setObj(team);
        }
        else {
            message.setMsg("服务器异常，请稍后再试");
            message.setCode(500);
        }
        return message;
    }

    /**
     * @description 上传球队logo
     * @author 30500
     * @date 2021/7/24 8:45
     * @type [org.springframework.web.multipart.MultipartFile, int, javax.servlet.http.HttpServletRequest]
     * @return com.beordie.bean.Message
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public Message uploadLogo(@RequestParam("logo") MultipartFile file, @PathVariable("id") int teamId, HttpServletRequest request) {
        String originalFilename = file.getOriginalFilename();
        String fileType = originalFilename.substring(originalFilename.lastIndexOf('.'));
        String realName = UUID.randomUUID().toString().replace("-","") + fileType;
        String realPath = request.getServletContext().getRealPath("/img/uploadLogo")+"/";
        System.out.println(realPath);
        System.out.println(realName);

        Message message = new Message();
        try {
            file.transferTo(new File(realPath + realName));
            Team team = new Team();
            team.setTeamLogo(realName);
            team.setTeamId(teamId);
            teamService.updateById(team);
            message.setMsg("上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            message.setMsg("图片上传出现问题！"+e.getMessage());
            message.setCode(500);
        }
        return message;
    }
}

















